package com.stylefeng.guns.rest.modular.wz.api.v3;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.rest.common.base.controller.ApiBaseController;
import com.stylefeng.guns.rest.common.constant.Consts;
import com.stylefeng.guns.rest.common.persistence.dao.WzUserMapper;
import com.stylefeng.guns.rest.common.persistence.model.WzUser;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.domain.Res;
import com.stylefeng.guns.rest.modular.wz.service.IWzUserService;
import com.stylefeng.guns.rest.modular.wz.util.wz.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @program: wz_dev_app_shop
 * @description: 市场部后台数据
 * @author: Chenchao
 * @create: 2018-07-23 13:34
 **/
@RestController
@RequestMapping("/wz/api/v3/marketingBackground/")
public class WzMarketingBackgroundApi extends ApiBaseController {
    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private IWzUserService wzUserService;
    @PostMapping("data")
    public Res orderList(@RequestBody ParamMap paramMap) {
        Double totalUserNumberPercent=Double.parseDouble(Consts.totalUserNumber)/100;
        Double activeUserNumberPercent=Double.parseDouble(Consts.activeUserNumber)/100;
        int totalNum=userMapper.selectCount(new EntityWrapper<>());
        paramMap.put("totalNum",Math.ceil(totalNum*totalUserNumberPercent));
//        int activityNum=userMapper.selectCount(new EntityWrapper<WzUser>().eq("user_activity",1));
        String x = DateUtil.toString(new Date()) + " 00:00:00";
        String y = DateUtil.toString(DateUtil.nextDay(new Date())) + " 00:00:00";
        int activityNum = userMapper.selectCount(new EntityWrapper<WzUser>().between("update_time",x,y));
        paramMap.put("activityNum",Math.ceil(activityNum*activeUserNumberPercent));
        //ios手机型号
        String[] iosMobileType={"iPhone 5s","iPhone 6","iPhone 6s","iPhone 7","iPhone8","iPhone10"};
        String[] iosMobileTypeView={"iPhone 5s","iPhone 6/6plus","iPhone 6s/6sPlus","iPhone 7/7Plus","iPhone 8/8Plus","iPhone X"};
        Integer iosOtherNum=0;
        List<Map<String,Object>> iosMobileTypeList=new ArrayList<>();
        for(int i=0;i<iosMobileType.length;i++){
            Integer num=0;
            if("iPhone 6".equals(iosMobileType[i])){
                num = wzUserService.selectCount(new EntityWrapper<WzUser>().like("login_info", iosMobileType[i]).notLike("login_info","iPhone 6s"));
            }else {
                num = wzUserService.selectCount(new EntityWrapper<WzUser>().like("login_info", iosMobileType[i]));
            }
            iosOtherNum+=num;
            Map<String, Object> map = new HashMap<>();
            map.put("value", num);
            map.put("name", iosMobileTypeView[i]);
            iosMobileTypeList.add(map);
        }
        Integer alliosNum = wzUserService.selectCount(new EntityWrapper<WzUser>().notLike("login_info", "手机型号"));
        Map<String, Object> Imap = new HashMap<>();
        Imap.put("value", alliosNum-iosOtherNum);
        Imap.put("name", "其它");
        iosMobileTypeList.add(Imap);
//        paramMap.put("iosType",iosMobileTypeList);
        //安卓手机型号
        String[] androidMobileType={"honor","xiaomi","samsung","meizu","vivo","oppo","huawei"};
        String[] androidMobileTypeView={"荣耀","小米","三星","魅族","vivo","OPPO","华为"};
        List<Map<String,Object>> androidMobileTypeList=new ArrayList<>();
        Integer androidOtherNum=0;
        for(int i=0;i<androidMobileType.length;i++){
            Integer num = wzUserService.selectCount(new EntityWrapper<WzUser>().like("login_info", androidMobileType[i]));
            androidOtherNum+=num;
            Map<String, Object> map = new HashMap<>();
            map.put("value", num);
            map.put("name", androidMobileTypeView[i]);
            androidMobileTypeList.add(map);
        }
        Integer allAndroidNum = wzUserService.selectCount(new EntityWrapper<WzUser>().like("login_info", "手机型号"));
        Map<String, Object> Amap = new HashMap<>();
        Amap.put("value", allAndroidNum-androidOtherNum);
        Amap.put("name", "其他");
        androidMobileTypeList.add(Amap);
//        paramMap.put("androidType",androidMobileTypeList);
        Map<String,Object> mobileType=new HashMap<>();
        mobileType.put("iosType",iosMobileTypeList);
        mobileType.put("androidType",androidMobileTypeList);
        paramMap.put("mobileType",mobileType);
//        int commonGenderNum=userMapper.selectCount(new EntityWrapper<WzUser>().eq("gender",0));
        int maleNum=userMapper.selectCount(new EntityWrapper<WzUser>().eq("gender",1));
        int femaleNum=userMapper.selectCount(new EntityWrapper<WzUser>().eq("gender",2));
        Map genderData=new HashMap();
//        genderData.put("commonGenderNum",commonGenderNum);
        genderData.put("男",maleNum);
        genderData.put("女",femaleNum);
        paramMap.put("genderData",genderData);
        return Res.success(paramMap);
    }

}
